/*
*	ID : Pallab
* 	PROG : E
* 	LANG : C++
* 	2012-11-24-13.36.47 Saturday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
    double endtime = clock() + (seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}
template<class T>
inline T fastIn() {
    register char c=0;
    register T a=0;
    bool neg=false;
    while(c<33)c=getchar();
    while(c>33) {
        if(c=='-') {
            neg=true;
        } else {
            a= (a*10)+(c-'0');
        }
        c=getchar();
    }
    return neg?-a:a;
}
inline void readword(char *buffer) {
    int c, cnt = 0;
    c = getchar();
    while (c >= 0 && c <= ' ') {
        c = getchar();
    }
    if (c < 0) {
        buffer[cnt] = 0;
        return;
    }
    while (c > ' ') {
        buffer[cnt++] = c;
        c = getchar();
    }
    buffer[cnt] = 0;
}

const int MX = (int)1e5*5;
map< pair<int,int>, int > memo;
string names[MX];
int people_count;
vector<int> adj[MX];
#define CLR(X) for(int i=0;i<MX;++i)(X)[i].clear()
#define F first
#define S second
inline void read() {
    char buff[25];
    people_count=fastIn<int>();
    int parent;
    CLR(adj);
    for(int i=1; i<=people_count; ++i) {
        readword(buff),parent=fastIn<int>();
        names[i]=(buff),adj[parent].pb(i);
    }
}
int tin[MX],tout[MX],timer;
vector<pair<int,int> > whoo[MX]; //ae depth'a koto gula node ase, and tara ke ke
int depth[MX]; // kon node'er depth koto aeta hisab rakhbo
inline void dfs(int parent, int cur_depth) {

    tin[parent]=++timer;
    depth[parent]=cur_depth;
    whoo[cur_depth].pb( mk(timer,parent) );
    foit(it, adj[parent]) {
        dfs(*it, cur_depth+1);
    }
    tout[parent]=++timer;
}
inline int get(int at, int start_time, int end_time) {
    vector<pair<int,int> >&time_segment = whoo[at];
    vector<pair<int,int> >:: iterator st = upper_bound(time_segment.begin(),time_segment.end(),mk(start_time,0));
    vector<pair<int,int> >:: iterator ed =lower_bound(time_segment.begin(),time_segment.end(),mk(end_time,0));
    set<string> names_count;
    for( ; st!=ed && st!=time_segment.end() ; st++) {
        names_count.insert( names[st->S] );
    }
    return SZ(names_count);
}
inline void proc() {
    SET(tin,0),SET(tout,0),timer=0;
    CLR(whoo);
    SET(depth,0);
    dfs(0,0);
    memo.clear();
    int query_count=fastIn<int>(),who,k_depth;
    for(int i=1; i<=query_count; ++i) {
        who=fastIn<int>(),k_depth=fastIn<int>();
        if(memo.count(mk(who,k_depth)) ) {
            cout<<memo[mk(who,k_depth)]<<"\n";
        } else {
            int start_time = tin[who],end_time=tout[who];
            int at = depth[who]+k_depth;
            int retval = get(at, start_time,end_time);
            memo[mk(who,k_depth)]=retval;
            cout<<retval<<'\n';
        }
    }
}
int main() {
    int kase = 1;
#if defined( xerxes_pc )
    if (!freopen("in1", "r", stdin))
        puts("error opening file in "), assert(0);
    kase = 1;
#endif
    fo(i,kase) {
        read();
        proc();
    }
    return 0;
}
